package com.ruoyi.business.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.business.domain.Product;
import com.ruoyi.business.domain.User;
import com.ruoyi.business.domain.vo.UserProductVo;
import com.ruoyi.business.mapper.ProductMapper;
import com.ruoyi.business.mapper.UserMapper;
import com.ruoyi.common.utils.SecurityUtils;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.UserProductMapper;
import com.ruoyi.business.domain.UserProduct;
import com.ruoyi.business.service.IUserProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

/**
 * 购物列表Service业务层处理
 * 
 * @author 吴倩芸
 * @date 2023-03-07
 */
@SuppressWarnings("ALL")
@Service
public class UserProductServiceImpl extends ServiceImpl<UserProductMapper, UserProduct> implements IUserProductService
{
    @Autowired
    private UserProductMapper userProductMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 查询购物列表
     * 
     * @param id 购物列表主键
     * @return 购物列表
     */
    @Override
    public UserProduct selectUserProductById(Long id)
    {
        return userProductMapper.selectUserProductById(id);
    }

    /**
     * 查询购物列表列表
     * 
     * @param userProduct 购物列表
     * @return 购物列表
     */
    @Override
    public List<UserProductVo> selectUserProductList(UserProduct userProduct)
    {
        Long userId = SecurityUtils.getUserId();
        if (!SecurityUtils.isAdmin(userId)) {
            // 如果当前不是超级管理员，就只能查询某个用户的购买记录
            userProduct.setUserId(userId);
        }
        List<UserProduct> userProducts = userProductMapper.selectUserProductList(userProduct);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        return userProducts.stream().map(item -> {
            UserProductVo vo = new UserProductVo();
            BeanUtils.copyProperties(item, vo);
            vo.setProduct(productMapper.selectProductById(item.getProductId()));
            vo.setUser(userMapper.selectUserById(item.getUserId()));
            vo.setBuyTime(sdf.format(item.getBuyTime()));
            return vo;
        }).collect(Collectors.toList());
    }

    /**
     * 新增购物列表
     * 
     * @param userProduct 购物列表
     * @return 结果
     */
    @Transactional
    @Override
    public int insertUserProduct(UserProduct userProduct)
    {
        Long userId = SecurityUtils.getUserId();
        if (userProduct.getUserId() == null) {
            userProduct.setUserId(userId);
        }
        if (userProduct.getBuyNum() == null || userProduct.getBuyNum() == 0) {
            throw new RuntimeException("请重新填写购买数量");
        }
        if (userProduct.getProductId() == null) {
            throw new RuntimeException("请重新填写购买表单");
        }
        Product product = productMapper.selectProductById(userProduct.getProductId());
        if (product == null) {
            throw new RuntimeException("请重新检查购买的物品");
        }
        userProduct.setAmount(userProduct.getBuyNum() * Double.parseDouble(product.getWorth()));
        // 检查当前用户的账户
        User user = userMapper.selectUserById(userId);
        if (Double.parseDouble(user.getMoney()) < userProduct.getAmount()) {
            throw new RuntimeException("抱歉您当前余额不足，请充值");
        }
        // 消费完成之后的余额
        Double surplus = Double.parseDouble(user.getMoney()) - userProduct.getAmount();
        user.setMoney(Double.toString(surplus));
        // 减少用户的余额
        userMapper.updateUser(user);
        // 减少库存数
        product.setStock(product.getStock() - userProduct.getBuyNum().intValue());
        productMapper.updateProduct(product);
        userProduct.setBuyTime(new Date());
        return userProductMapper.insertUserProduct(userProduct);
    }

    /**
     * 修改购物列表
     * 
     * @param userProduct 购物列表
     * @return 结果
     */
    @Override
    public int updateUserProduct(UserProduct userProduct)
    {
        return userProductMapper.updateUserProduct(userProduct);
    }

    /**
     * 批量删除购物列表
     * 
     * @param ids 需要删除的购物列表主键
     * @return 结果
     */
    @Override
    public int deleteUserProductByIds(Long[] ids)
    {
        return userProductMapper.deleteUserProductByIds(ids);
    }

    /**
     * 删除购物列表信息
     * 
     * @param id 购物列表主键
     * @return 结果
     */
    @Override
    public int deleteUserProductById(Long id)
    {
        return userProductMapper.deleteUserProductById(id);
    }
}
